<?php
/**
* 
*/
class ReportModel extends MY_Model
{
	public function __construct()
	{
		parent::__construct();
		$this->table = 'nmp_user_appoint';
	}


	//获取列表数据
	public function get_list_data($term='',$page=1,$size=10)
	{
		//查询搜索
		$where = ' where ap.status = 1 ';

		if (isset($term['keyword']) && !empty($term['keyword'])) {

			$where .= " AND (u.nickname LIKE '%".$term['keyword']."%' OR u.mobile LIKE '%".$term['keyword']."%')";
		}

		$query = $this->db->query("SELECT ap.*, u.nickname, u.mobile, u.headimg, c.name as course_name, ca.card_name, card.price, s.name as shop_name, co.nickname as coach_name FROM ".$this->table." AS ap 
				LEFT JOIN nmp_user AS u ON ap.user_id = u.id 
				LEFT JOIN nmp_user_card AS card ON ap.card_id = card.id
				LEFT JOIN nmp_course AS c ON ap.cid = c.id 
				LEFT JOIN nmp_coach AS co ON ap.mid = co.id 
				LEFT JOIN nmp_user_card AS uc ON ap.card_id = uc.id 
				LEFT JOIN nmp_course_card AS ca ON ap.card_id = ca.id
				LEFT JOIN nmp_shop AS s ON ap.shop_id = s.id 
				
				".$where." ORDER BY ap.sdate DESC  LIMIT ".($page-1)*$size.",".$size);
				
				
		$data = $query->result_array();
		

		foreach ($data as $key => $value) {
			$data[$key]['addtime'] = date('Y-m-d H:i:s', $value['addtime']);
			$data[$key]['card_name'] = $value['card_name']?$value['card_name']:'';
			//$data[$key]['price'] = $value['price']?$value['price']:'0';
			$data[$key]['course_name'] = $value['course_name']?$value['course_name']:'';
		}
		
		$query = $this->db->query("select COUNT(*)  as num from ".$this->table." AS ap 
			LEFT JOIN nmp_user AS u ON ap.user_id = u.id 
			LEFT JOIN nmp_course AS c ON ap.cid = c.id 
			LEFT JOIN nmp_coach AS co ON ap.mid = co.id 
			LEFT JOIN nmp_user_card AS uc ON ap.card_id = uc.id 
			LEFT JOIN nmp_course_card AS ca ON uc.card_id = ca.id
			LEFT JOIN nmp_shop AS s ON ap.shop_id = s.id 
			".$where);
		$row = $query->row_array();

		//总数
		$count = $row['num'];
		//总页数
		$pages = ceil($count/$size);
		
		return array('list'=>$data,'count'=>$count, 'pages'=>$pages);

	}


	//获取会员
	public function get_user($user)
	{
		if (!empty($user)) 
		{
			$query = $this->db->query("SELECT id, nickname, mobile FROM nmp_user WHERE (nickname LIKE '%".$user."%' OR mobile LIKE '%".$user."%')");
			$data = $query->result_array();

			return $data;
		}else{
			return array();
		}

	}

	//获取会员信息和预约信息
	public function appoint_data($user_id)
	{		
		$this->db->select('nickname, headimg, sex, mobile, remarks');
		$this->db->from('nmp_user');
		$this->db->where('id',$user_id);
		$query = $this->db->get();
		$data = $query->row_array();

		$data['sex'] = $data['sex'] == 1 ? '男' : '女';

		$query= $this->db->query("SELECT ap.*, c.name as course_name, ca.card_name, ca.price, s.name as shop_name, ch.nickname as coach_name FROM ".$this->table." AS ap 
				LEFT JOIN nmp_course AS c ON ap.cid = c.id 
				LEFT JOIN nmp_user_card AS uc ON ap.card_id = uc.id 
				LEFT JOIN nmp_course_card AS ca ON uc.card_id = ca.id 
				LEFT JOIN nmp_shop AS s ON ap.shop_id = s.id 
				LEFT JOIN nmp_coach AS ch ON ap.mid = ch.id 
				WHERE ap.user_id = '$user_id' AND ap.status = 0");
		$data['appoint_list'] = $query->result_array();

		foreach ($data['appoint_list'] as $key => $value) {
			$data['appoint_list'][$key]['addtime'] = date('Y-m-d H:i:s', $value['addtime']);
		}

		return $data;
	}

	//添加签到
	public function update_appoint($arr=array(),$appoint_id)
	{
		if ($this->db->update($this->table,$arr,array('id'=>$appoint_id))){			
			return true;
		}else{
			return false;
		}
	}

	//取消签到
	public function cancel_user_appoint($arr=array(),$appoint_id)
	{
		if ($this->db->update($this->table,$arr,array('id'=>$appoint_id))){			
			return true;
		}else{
			return false;
		}

	}


}



?>